﻿def uploads_pic(request):
    if request.method == 'GET':
        return render(request,'uploads.html/')
    else:
        # -*- coding: utf-8 -*-
        # flake8: noqa
        from qiniu import Auth, put_file, etag
        import qiniu.config
        # 需要填写你的 Access Key 和 Secret Key
        access_key = 'dGeYKT8e8dPmKkC9zxgyW_awTFJDCRhNmeyyFKHJ'
        secret_key = 'hvj7m8SUH_-9JzoApvVoN08r_8qryvbsLZG2fJIz'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = 'twzmappic'
        # 上传后保存的文件名

        key = 'my-python2-logo.png'
        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name, key, 3600)
        # 要上传文件的本地路径
        # localfile = request.POST.get('file')
        localfile = request.FILES['file']
        # import os
        # res = os.path.join(localfile, 'localfile')
        # print(res)
        # localfile = '111.png'
        ret, info = put_file(token, key, localfile)
        print(info)
        assert ret['key'] == key
        assert ret['hash'] == etag(localfile)

        print('info',info.key)

        return render(request, 'uploads.html/',{'info':info})
